#if 0
/* 该模块为测试模块，正式发布之后会去除 */
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>                         
#include <unistd.h>  
#include <errno.h>
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>
#include <pthread.h>
#include "locketdef.h"
#include "locketerrno.h"
#include "locketlog.h"
#include "locketcommon.h"

#define FORWARD_IP "192.168.10.51"

#define FORWARD_PORT 55056

int gForwardSocket = 0;

static const int MAXPENDING = 50;  
  
#define BUFSIZE 1024
  
void HandleTCPClient(int clntSocket);  
  
void DieWithUserMessage(const char *msg, const char *detail);  
  
void DieWithSystemMessage(const char *msg);  
  
int LOCKET_DA_InitClient ()
{
	// Create local socket  
    int sock;  
    if ( ( sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 ) {  
        DieWithSystemMessage("socket() failed.");  
    }   
    int bReuse = 0;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &bReuse, sizeof(bReuse));
  
    int servPort = FORWARD_PORT;
  
    // Create server address  
    struct sockaddr_in servAddr;  
    memset( &servAddr, 0, sizeof(servAddr) );  
    servAddr.sin_family = AF_INET;  
    servAddr.sin_port = htons(servPort);  
    inet_pton(AF_INET, FORWARD_IP, &servAddr.sin_addr.s_addr);  //转发目的服务器IP地址
  
    // Connect to server  
    do 
    {
        if ((connect( sock, (struct sockaddr *) &servAddr, sizeof(servAddr))) < 0  ) {  
            if (EINTR == errno)
            {
                LOCKET_LOGERROR ("connect interupted , retry\n");
                continue;
            }
            else
            {
                DieWithSystemMessage("connect() failed.");  
            }
        }  
        break;
    } while (1);

    return sock;
}

void* thread_func (void *param)
{
    int port = *(int*) param;
    gForwardSocket = LOCKET_DA_InitClient ();
    if (gForwardSocket < 0)
    {
        LOCKET_LOGERROR ("connect forward server failed");
        return NULL;
    }
    LOCKET_LOGERROR ("Init client success");
    int servPort = port;
  
    // Create socket for incoming connections  
    int servSock;  
    if((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ) {  
        DieWithSystemMessage("Socket() failed");  
    }  
  
    // Construct local address structure  
    struct sockaddr_in servAddr;            // Local address  
    memset(&servAddr, 0, sizeof(servAddr));     // Zero out structure  
    servAddr.sin_family =  AF_INET;         // IPv4 address family  
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);   // Any incoming interface.  
    servAddr.sin_port = htons(servPort);  
      
    LOCKET_LOGERROR ("socket is :%d, port:%d", servSock, port);
    //Bind to the local address  
    if (bind(servSock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0 )  
        DieWithSystemMessage("bind() failed");  
  
    // Mark the socket so it will listen for incoming connections  
    LOCKET_LOGERROR ("listen ok");
    if (listen(servSock, MAXPENDING) < 0 )  
        DieWithSystemMessage("listen() failed");  
  
    for (;;) {                  // Run forever  
        struct sockaddr_in clntAddr;        // Client address  
        // Set length of client address structure (in-out parameter)  
        socklen_t clntAddrLen = sizeof(clntAddr);  
          
        // Wait for a client to connect  
        LOCKET_LOGERROR ("accept....");
        int clntSock = accept(servSock, (struct sockaddr*) &clntAddr, &clntAddrLen);  
        LOCKET_LOGERROR ("clntsocket :%d", clntSock);
        if (clntSock < 0 )  
        {
            if (errno == EINTR)
            {
                LOCKET_LOGERROR ("eintr");
                continue;
            }
            else
            {
                DieWithSystemMessage("accept() failed");  
            }
        }
        LOCKET_LOGERROR ("accept ok");
          
        // clntSock is connected to a client!  
          
        char clntName[INET_ADDRSTRLEN];     // String to contain client address  
        if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName, sizeof(clntName)) != NULL )  
            LOCKET_LOGERROR("Handling client %s/%d\n", clntName, ntohs(clntAddr.sin_port));  
        else  
            LOCKET_LOGERROR("Unable to get client address");  
  
        HandleTCPClient(clntSock);  
        
    }  
    LOCKET_LOGERROR("thread exit");

}

int LOCKET_DA_Init(int port) {  

    pthread_t threadID = 0;
    if (0 != pthread_create (&threadID, NULL, thread_func, &port))
    {
        LOCKET_LOGERROR ("create thread failed\n");
        return -1;
    }
    LOCKET_LOGERROR ("create thread success");

    sleep (2);

    return 0;
}  
  
void HandleTCPClient(int clntSocket){  
    char buffer[BUFSIZE]= {0};   // Buffer for echo string  
      
    // Receive message from client.  
    ssize_t numBytesRcvd = recv(clntSocket, buffer, BUFSIZE, 0);  
    if ( numBytesRcvd < 0 )  
        DieWithSystemMessage("recv() failed");  
  
    LOCKET_LOGERROR ("recv msg:%s, numBytesRcvd:%d", buffer, numBytesRcvd);
    // Send received string and receive again until end of stream.  
    while ( numBytesRcvd > 0 ) {  //0 indicates end of stream.  
        // Echo message back to client.  
        LOCKET_LOGERROR ("forward msg, buffer:%s", buffer);
        ssize_t numBytesSent = send(gForwardSocket, buffer, numBytesRcvd, 0);  
        if ( numBytesSent < 0 )  
            DieWithSystemMessage("send() failed");  
        else if ( numBytesSent != numBytesRcvd )  
            DieWithUserMessage("send()", "send unexpected number of bytes");  
          
        // See if there is more data to receive.  
        memset (buffer, 0, sizeof (buffer));
        numBytesRcvd = recv(clntSocket, buffer, BUFSIZE, 0);  
        if ( numBytesRcvd < 0 )  
            DieWithSystemMessage("recv() failed.");  
        else
        {
            LOCKET_LOGERROR ("recv string:%s, num:%ld\n", buffer, numBytesRcvd);
        }
    }  
      
    close(clntSocket);  // Close client socket.  
}  
  
void DieWithUserMessage(const char *msg, const char *detail) {  
    LOCKET_LOGERROR("%s:%s", msg, detail);
    return;
}  
  
void DieWithSystemMessage(const char *msg) {  
    LOCKET_LOGERROR("%s:%s", msg, strerror(errno));
    return;
}  


#endif
